
let playMusicId = {};

var NativeSound = cc.Class({

    statics: {

        /**
         * 播放音效
         * @param fileName
         */
        playEffect:function(fileName){
            let url = this.getFileUrl(fileName, true);
            cc.loader.load(url,  function (err, res) {
                if (err) {
                    Log.error(err.message || err);
                    return;
                }
                cc.audioEngine.play(res);
            });
        },

        playMusic:function(fileName,isLoop){
            var url = 'Sound/' + fileName;
            let self = this;
            cc.loader.loadRes(url, cc.AudioClip, function (err, res) {
                if (err) {
                    Log.error('---------loadRes err: ' + err);
                    return;
                }
                playMusicId[fileName] = cc.audioEngine.play(res, isLoop);
            });
        },

        stopMusic:function(fileName){
            if(!fileName){
                cc.audioEngine.stopAll();
                return;
            }
            cc.audioEngine.stop(playMusicId[fileName]);
            playMusicId[fileName] = -1;
        },

        /**
         * 暂停音乐
         * @param fileName
         */
        pauseMusic: function(fileName){
            if(fileName){
                cc.audioEngine.pause(playMusicId[fileName]);
            }
            cc.audioEngine.pauseAll();
        },

        /**
         * 恢复音乐播放
         * @param fileName
         */
        resumeMusic:function(fileName){
            if(fileName){
                cc.audioEngine.resume(playMusicId[fileName]);
                return;
            }
            cc.audioEngine.resumeAll();
        },



        getFileUrl: function(fileName, isAssets){
            if(fileName == ''){
                return '';
            }
            if(isAssets){
                return cc.url.raw('resources/Sound/' + fileName);
            }
            return fileName;
        },

    }
});

module.exports = NativeSound;